{{--
/**
 * Created by PhpStrom
 * User: 郭碧云
 * Date: 2021/7/12 9:03
 * Desc:
 */
--}}
@extends("layout.admin")

@section("title")
    试题列表
@endsection

@section("content")
    {{--修改样式--}}
    <link href="{{asset('css/table.css')}}" rel="stylesheet"/>
    <style>
        .tag_show .layui-form-select, .version_show .layui-form-select {
            width: 166% !important;
        }

        .layui-input {
            color: #999999 !important;
        }
    </style>
    <div class="layui-card-body ">
        {{--搜索--}}
        <form class="layui-form layui-col-space5 layui-input-inline" id="search-form" style="width: 100%;">
            <div class="demoTable">
                <div>
                    <div class="layui-inline">
                        <input class="layui-input" name="question" id="question" style="width: 100% !important;"
                               value="{{request('question')}}" placeholder="请输入题目">
                    </div>
                    <div class="layui-input-inline" style="width: 12%;">
                        <div id="type_id"></div>
                    </div>
                    <div class="layui-input-inline" style="width: 12%;">
                        <select id="level" name="level">
                            {!! Code::getOptionsByCode('QUESTION_LEVEL',request('level'),'--难度--') !!}
                        </select>
                    </div>
                    <div class="layui-input-inline" style="width: 12%;margin-left: -79px;">
                        <div id="creator"></div>
                    </div>
                    <div class="layui-input-inline" style="width: 12%;">
                        <div id="org"></div>
                    </div>

                    <div id="category_id" class="layui-input-inline" style="width: 12%"></div>
                    <div id="version_show" class="layui-inline version_show" style="width: 12%">
                        <select>
                            <option>--版本--</option>
                        </select>
                    </div>
                    <div id="tag_show" class="layui-inline tag_show" style="width: 12%">
                        <select>
                            <option>--标签--</option>
                        </select>
                    </div>
                    <div id="version" class="layui-input-inline layui-hide" style="width: 12%"></div>
                    <div id="tag_id" class="layui-input-inline layui-hide" style="width: 12%"></div>

                    <div class="layui-inline layui-show-xs-block">
                        <a class="layui-btn" lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></a>
                    </div>
                </div>
            </div>

        </form>
    </div>
    {{--列表--}}
    <div class="layui-card-body">
        <script type="text/html" id="toolbar">
            {{--添加--}}

            <div class="layui-inline layui-show-xs-block">
                @if(_can("admin.question.create"))
                    <a href="{{ route("admin.question.create") }}"
                       class="layui-btn layui-btn-sm" data-type="auto" id="create"><i class="layui-icon"></i>添加</a>
                @endif
                @if(_can("admin.question.destroy.attach"))
                    <a class="layui-btn layui-btn-sm"
                       data-type="auto" lay-submit="" lay-filter="delMore"><i class="layui-icon">&#xe640;</i>批量删除</a>
                @endif

                <a class="layui-btn layui-btn-sm" data-type="auto" id="import"><i class="layui-icon"></i>导入</a>
                <a class="layui-btn layui-btn-sm" data-type="auto" id="export"><i class="layui-icon"></i>导出</a>
            </div>

        </script>
        <table id="listT" class="layui-table" lay-filter="table-check"
               lay-data="{toolbar: '#toolbar',defaultToolbar:['filter'], limit:  {{$question['per_page']}} }">
            <thead>
            <tr>
                <th lay-data="{type: 'checkbox', fixed: 'left'}"></th>
                <th lay-data="{field:'id',width:80, fixed: 'left', unresize: true, sort: true}">编号</th>
                <th lay-data="{field:'category_id'}">科目</th>
                <th lay-data="{field:'type_id'}">题型</th>
                <th lay-data="{field:'question'}">题目</th>
                <th lay-data="{field:'level'}">等级</th>
                <th lay-data="{field:'creator'}">录题人</th>
                <th lay-data="{field:'org_id'}">组织机构</th>
                <th lay-data="{field:'tag_id'}">标签</th>
                <th lay-data="{field:'version_id'}">版本</th>
                <th lay-data="{field:'created_at',width:200,hide:true}">创建时间</th>
                <th lay-data="{field:'updated_at',width:200,hide:true}">更新时间</th>
                <th lay-data="{field:'op',fixed: 'right', width:150, align:'center'}">操作</th>
            </tr>
            </thead>
            <tbody>
            @foreach ($question["data"] as $q)
                <tr>
                    <td></td>
                    <td>{{ $q['id'] }}</td>
                    <td>{{ $q['category']['name'] }}</td>
                    <td>{{ $q['type']['name'] }}</td>
                    <td><a href="javascript:;"
                           onclick="admin.openLayerShowForm('{{ route("admin.question.show",["id"=>$q["id"]]) }}', '试题详情', '500px', 'auto','show_q_from')">   {!! $q['question'] !!} </a>
                    </td>
                    <td>{{ \App\Facades\Code::name("QUESTION_LEVEL",$q['level']) }}</td>
                    <td>{{ $q['user']['real_name'].'('.$q['user']['name'].')' }}</td>
                    <td>{{ $q['org']['name'] }}</td>
                    <td>{{ empty($q["tag"])?"":$q['tag']['name'] }}</td>
                    <td>{{ $q['version']['version']}}</td>
                    <td>{{ $q['created_at']}}</td>
                    <td>{{ $q['updated_at']}}</td>
                    <td>
                        <a class="layui-btn layui-btn-xs"
                           href="{{route('admin.question.update',['id'=>$q['id']])}}">编辑</a>
                        <a class="layui-btn layui-btn-xs"
                           onclick="admin.tableDataDelete('{{route('admin.question.destroy',['ids'=>$q['id']])}}',this,'是否删除该试题')">删除</a>
                    </td>
                </tr>
            @endforeach
            </tbody>
        </table>
        @include("layout.page")

    </div>
@endsection

@section('script')
    <script src="{{asset('layui/modules/xm-select.js')}}"></script>
    <script src="{{asset('js/tree/select.js')}}"></script>
    <script>
        var idx = -1;
        var category_id = "{!! request('category_id') !!}";
        var categoryData = JSON.parse('{!! $category !!}');
        layui.use(['form', 'table', 'layer'], function () {
            var table = layui.table,
                form = layui.form,
                xmSelect = layui.xmSelect;

            //题型
            let type = selectTreeParams('#type_id', 'type_id', '--题型--', "{{request('type_id')}}", JSON.parse('{!! $types !!}'), res => function () {
            });
            xmSelect.render(type)

            //添加人
            let creator = selectTreeParams('#creator', 'creator', '--添加人--', "{{request('creator')}}", JSON.parse('{!! $creators !!}'), res => function () {
            }, 'rname');
            xmSelect.render(creator)

            //组织机构
            let org = selectTreeParams('#org', 'org_id', '--组织机构--', "{{request('org_id')}}", JSON.parse('{!! $orgs !!}'), res => function () {
            });
            xmSelect.render(org)

            //科目
            let category = selectTreeParams('#category_id', 'category_id', '--科目--', "{{request('category_id')}}", categoryData, (res) => {
                let tags = res.arr[0].exam_tag_group.length > 0 ? res.arr[0].exam_tag_group[0].tags : []
                let versions = res.arr[0].version.length > 0 ? res.arr[0].version : []
                __tags("{{request('tag_id')}}", tags);
                __version("{{request('version_id')}}", versions)

            });
            xmSelect.render(category)

            //标签
            if (category_id > 0) {
                let tags = {};
                let version = {};
                categoryData.forEach((item) => {
                    if (item.id == category_id) {
                        tags = item.exam_tag_group[0].tags
                        version = item.version
                    }
                })
                __version("{{request('version_id')}}", version);
                __tags("{{request('tag_id')}}", tags);
            }

            //分页显示
            admin.paginate("{{ $question['total'] }}", "{{ $question['current_page'] }}", "{{ $question['per_page'] }}");

            //批量删除
            form.on("submit(delMore)", function () {
                let del=[];
                let checkBox=table.checkStatus('listT').data
                checkBox.forEach((item)=>{
                    del.push(parseInt(item["id"]))
                })

                layer.confirm("是否批量删除试题", {
                    btn: ['删除', '取消']
                }, function () {
                    admin.send('{{route('admin.question.destroy')}}','GET',{ids:del},2)
                }, function () {
                    layer.close();
                });
            })
        });

        function __tags(tag_id, tags) {
            $("#tag_show").hide();
            $("#tag_id").removeClass("layui-hide");
            let tag = selectTreeParams('#tag_id', 'tag_id', '--标签--', tag_id, tags, (res) => {
            });
            xmSelect.render(tag)
        }

        function __version(version_id, version) {
            $("#version_show").hide();
            $("#version").removeClass("layui-hide");
            //版本
            let versions = selectTreeParams('#version', 'version_id', '--版本--', version_id, version, res => function () {
            }, 'version');
            xmSelect.render(versions)
        }
    </script>

    <script src="{{asset('js/layCommon.js')}}"></script>
@endsection

